package com.thread.collection.Map;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.CountDownLatch;

public class ConcurrentMapTest {

    static Map<String,String> map = new ConcurrentHashMap<String,String>();//20668
    //static Map<String,String> map = new ConcurrentSkipListMap<>();//23884
    //static Map<String,String> map = new HashMap<>();


    public static void main(String[] args) {
        Random r = new Random();
        Thread[] ts = new Thread[100];
        CountDownLatch c = new CountDownLatch(ts.length);
        System.out.println(map.size());
        long start = System.currentTimeMillis();
        for(int i=0;i<ts.length;++i){
            ts[i] = new Thread(()->{
               for(int j=0;j<1000000;++j){
                   map.put("key"+r.nextInt(100000),"v"+r.nextInt(10000000));
               }
               c.countDown();
            });
        }

        Arrays.asList(ts).forEach(t->t.start());

        try {
            c.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(System.currentTimeMillis()-start);
        System.out.println(map.size());
    }
}
